package com.example.xwuser.api;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.xwcommon.entity.User;
import com.example.xwcommon.service.UserService;
import com.example.xwcommon.service.WeiboService;
import com.example.xwcommon.vo.Response;
import io.seata.core.context.RootContext;
import io.seata.core.exception.TransactionException;
import io.seata.spring.annotation.GlobalTransactional;
import io.seata.tm.api.GlobalTransactionContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.UUID;

@RestController
@RefreshScope
public class UserApi {
    @Autowired
    @Qualifier("userService")
    private UserService userService;
    @Value("${fileLocation.uploag_location}")
    private String upload_location;
    @Autowired
    @Qualifier("com.example.xwcommon.service.WeiboService")
    private WeiboService weiboService;
    @RequestMapping("/api/user/reg")
    @GlobalTransactional
    public ResponseEntity<Response<User>> reg(String nickName, String loginName, String password, MultipartFile photo) throws IOException, TransactionException {
        String photoName=photo.getOriginalFilename();
        if(!photo.isEmpty()){
            photoName= UUID.randomUUID().toString()+photoName.substring(photoName.lastIndexOf("."));
            if(!new File(upload_location).exists()){
                new File(upload_location).mkdirs();
            }
            photo.transferTo(new File(upload_location +"/"+ photoName));
        }try{
            Response<User> response=userService.reg(nickName, loginName, password, photoName);
            if(response.getCode()==200){
                User user=response.getData();
                user.setUserPassword(null);
                response.setData(user);
                weiboService.addWeibo(user.getLoginName(),"新人报到","大家好，我是"+user.getNickname()+"很高兴认识大家！","");
                return ResponseEntity.ok(response);
            }
        }catch(RuntimeException e){
            GlobalTransactionContext.reload(RootContext.getXID()).rollback();
            return ResponseEntity.status(500).body(new Response<>(500,"参数错误",null));
        }
        return ResponseEntity.status(500).body(new Response<>(500,"参数错误",null));
    }

    @RequestMapping("/api/user/login")
    public ResponseEntity<Response<User>> login(String loginName, String password, HttpSession session) {
        Response<User> response=userService.login(loginName, password);
        if(response.getCode()==200){
            User user=response.getData();
            user.setUserPassword(null);
            response.setData(user);
            session.setAttribute("cur_user",user);
            return ResponseEntity.ok(response);
        }
        return ResponseEntity.status(500).body(new Response<>(500,"参数错误",null));
    }
    @RequestMapping("/api/user/updateState")
    public ResponseEntity<Response<User>> updateState(String loginName, Integer state) {
        Response<User> response=userService.updateState(loginName, state);
        if(response.getCode()==200){
            User user=response.getData();
            user.setUserPassword(null);
            response.setData(user);
            return ResponseEntity.ok(response);
        }
        return ResponseEntity.status(401).body(response);
    }
    @RequestMapping("/api/user/getAllUser")
    public ResponseEntity<Response<List<User>>> getAllUser() {
        Response<List<User>> response=userService.getAllUser();
        if(response.getCode()==200){
            return ResponseEntity.ok(response);
        }
        return ResponseEntity.status(401).body(response);
    }
}
